package com.xbongbong.pro.statistics.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.statistic.pojo.dto.DataSetAttrGetDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataSetAttrReflectJoinDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataSetAttrReflectUnionDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataSetBoardGetDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataSetBoardSaveDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataSetCheckPermissionDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataSetConfigExecuteDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataSetConfigGetDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataSetConfigSaveDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataSetCopyDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataSetDeleteDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataSetListDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataSetPermissionGetDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataSetPermissionSaveDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataSetRenameDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataSetRuleDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataSetSaveDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataSetSourceAttrDTO;
import com.xbongbong.pro.statistic.pojo.dto.FormIdBusinessTypeAttrDTO;
import com.xbongbong.pro.statistic.pojo.vo.DataSetAttrReflectJoinVO;
import com.xbongbong.pro.statistic.pojo.vo.DataSetAttrReflectUnionVO;
import com.xbongbong.pro.statistic.pojo.vo.DataSetBoardGetVO;
import com.xbongbong.pro.statistic.pojo.vo.DataSetConfigGetVO;
import com.xbongbong.pro.statistic.pojo.vo.DataSetCopyVO;
import com.xbongbong.pro.statistic.pojo.vo.DataSetListVO;
import com.xbongbong.pro.statistic.pojo.vo.DataSetPermissionGetVO;
import com.xbongbong.pro.statistic.pojo.vo.DataSetRuleVO;
import com.xbongbong.pro.statistic.pojo.vo.DataSetSaveVO;
import com.xbongbong.pro.statistic.pojo.vo.DataSourceAttrVO;
import com.xbongbong.pro.statistic.pojo.vo.NumberAttrVO;
import com.xbongbong.pro.statistic.service.ChartService;
import com.xbongbong.pro.statistic.service.DataSetService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 数据集
 * @author zcp
 * @version v1.0
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/dataSet")
public class DataSetController {
    private static final Logger LOG = LoggerFactory.getLogger(DataSetController.class);
    @Resource
    private DataSetService dataSetService;
    @Resource
    private ChartService chartService;

    /**
     * 数据集列表按钮权限校验
     * @param dataSetCheckPermissionDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/checkPermission", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String checkPermission(@RequestBody @Valid DataSetCheckPermissionDTO dataSetCheckPermissionDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = dataSetService.checkPermission(dataSetCheckPermissionDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 数据集列表
     * @param dataSetListDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid DataSetListDTO dataSetListDTO, BindingResult br) {
        XbbResponse<DataSetListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                //请求参数后处理，比如trim
                dataSetListDTO.postProcess();
                DataSetListVO salesReportVo = dataSetService.list(dataSetListDTO);
                response = new XbbResponse<>(salesReportVo);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 数据集新建的保存（新建未设置画布也先入库生成数据集，确定后再去设置画布）
     * @param dataSetSaveDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String save(@RequestBody @Valid DataSetSaveDTO dataSetSaveDTO, BindingResult br) {
        XbbResponse<DataSetSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DataSetSaveVO dataSetSaveVO = dataSetService.save(dataSetSaveDTO);
                response = new XbbResponse<>(dataSetSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 重命名
     * @param dataSetRenameDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/rename", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String rename(@RequestBody @Valid DataSetRenameDTO dataSetRenameDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = dataSetService.rename(dataSetRenameDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 数据集删除
     * @param dataSetDeleteDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid DataSetDeleteDTO dataSetDeleteDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = dataSetService.delete(dataSetDeleteDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 复制
     * @param dataSetCopyDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/copy", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String copy(@RequestBody @Valid DataSetCopyDTO dataSetCopyDTO, BindingResult br) {
        XbbResponse<DataSetCopyVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DataSetCopyVO baseVO = dataSetService.copy(dataSetCopyDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 权限获取
     * @param dataSetPermissionGetDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/permission/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String permissionGet(@RequestBody @Valid DataSetPermissionGetDTO dataSetPermissionGetDTO, BindingResult br) {
        XbbResponse<DataSetPermissionGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DataSetPermissionGetVO dataSetPermissionGetVO = dataSetService.permissionGet(dataSetPermissionGetDTO);
                response = new XbbResponse<>(dataSetPermissionGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 权限保存
     * @param dataSetPermissionSaveDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/permission/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String permissionSave(@RequestBody @Valid DataSetPermissionSaveDTO dataSetPermissionSaveDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = dataSetService.permissionSave(dataSetPermissionSaveDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 更新规则获取
     * @param dataSetConfigGetDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/config/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String configGet(@RequestBody @Valid DataSetConfigGetDTO dataSetConfigGetDTO, BindingResult br) {
        XbbResponse<DataSetConfigGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DataSetConfigGetVO dataSetConfigGetVO = dataSetService.configGet(dataSetConfigGetDTO);
                response = new XbbResponse<>(dataSetConfigGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 更新规则保存
     * @param dataSetConfigSaveDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/config/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String configSave(@RequestBody @Valid DataSetConfigSaveDTO dataSetConfigSaveDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = dataSetService.configSave(dataSetConfigSaveDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 手动触发更新
     * @param dataSetConfigExecuteDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/config/execute", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String configExecute(@RequestBody @Valid DataSetConfigExecuteDTO dataSetConfigExecuteDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = dataSetService.configExecute(dataSetConfigExecuteDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 画布获取
     * @param dataSetBoardGetDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/board/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String boardGet(@RequestBody @Valid DataSetBoardGetDTO dataSetBoardGetDTO, BindingResult br) {
        XbbResponse<DataSetBoardGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DataSetBoardGetVO dataSetBoardGetVO = dataSetService.boardGet(dataSetBoardGetDTO);
                response = new XbbResponse<>(dataSetBoardGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 画布保存
     * @param dataSetBoardSaveDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/board/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String boardSave(@RequestBody @Valid DataSetBoardSaveDTO dataSetBoardSaveDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                //默认给变更的状态
                if(Objects.isNull(dataSetBoardSaveDTO.getEditFlag())){
                    dataSetBoardSaveDTO.setEditFlag(true);
                }
                BaseVO baseVO = dataSetService.boardSave(dataSetBoardSaveDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 数据集规则封装，用于前端各种限制
     * @param dataSetRuleDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/rule", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String rule(@RequestBody @Valid DataSetRuleDTO dataSetRuleDTO, BindingResult br) {
        XbbResponse<DataSetRuleVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DataSetRuleVO dataSetRuleVO = dataSetService.rule(dataSetRuleDTO);
                response = new XbbResponse<>(dataSetRuleVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 字段生成/映射 - 合并节点
     * @param dataSetAttrReflectUnionDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/attrReflect/union", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String attrReflectUnion(@RequestBody @Valid DataSetAttrReflectUnionDTO dataSetAttrReflectUnionDTO, BindingResult br) {
        XbbResponse<DataSetAttrReflectUnionVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DataSetAttrReflectUnionVO dataSetAttrReflectUnionVO = dataSetService.attrReflectUnion(dataSetAttrReflectUnionDTO);
                response = new XbbResponse<>(dataSetAttrReflectUnionVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 字段生成/映射，并组装field - 连接节点
     * @param dataSetAttrReflectJoinDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/attrReflect/join", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String attrReflectJoin(@RequestBody @Valid DataSetAttrReflectJoinDTO dataSetAttrReflectJoinDTO, BindingResult br) {
        XbbResponse<DataSetAttrReflectJoinVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DataSetAttrReflectJoinVO dataSetAttrReflectJoinVO = dataSetService.attrReflectJoin(dataSetAttrReflectJoinDTO);
                response = new XbbResponse<>(dataSetAttrReflectJoinVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 字段获取 - 某个form
     * 目前是处理了一下入参，然后本质调用的bi内/pro/v1/chart/dataSourceAttr接口的实现方法，即ChartServiceImpl#dataSourceAttr
     * @param dataSetSourceAttrDTO
     * @param br
     * @return java.lang.String
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/form/dataSourceAttr", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String dataSourceAttr(@RequestBody @Valid DataSetSourceAttrDTO dataSetSourceAttrDTO, BindingResult br) {
        XbbResponse<DataSourceAttrVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DataSourceAttrVO dataSourceAttrVO = chartService.dataSetSourceAttr(dataSetSourceAttrDTO);
                response = new XbbResponse<>(dataSourceAttrVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 用于图表中心
     * @param dataSetAttrGetDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/dataSourceAttr", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String  sourceAttrAttr(@RequestBody @Valid DataSetAttrGetDTO dataSetAttrGetDTO, BindingResult br) {
        XbbResponse<DataSourceAttrVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DataSourceAttrVO dataSourceAttrVO = dataSetService.sourceAttrAttr(dataSetAttrGetDTO);
                response = new XbbResponse<>(dataSourceAttrVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 图表设计页面 可选计算字段列表
     *
     * @param formIdBusinessTypeAttrDTO
     * @param br
     * @return java.lang.String
     * @author zyy
     * @date 2022/10/25
     * @version v1.0
     */
    @RequestMapping(value = "/searchNumberAttr", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String searchDsNumberAttr(@RequestBody @Valid FormIdBusinessTypeAttrDTO formIdBusinessTypeAttrDTO, BindingResult br) {
        XbbResponse<NumberAttrVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                NumberAttrVO numberAttrVO = dataSetService.searchDsNumberAttr(formIdBusinessTypeAttrDTO);
                response = new XbbResponse<>(numberAttrVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
